package com.ruicar.afs.cloud.apply.pre.loan.service.impl;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.apply.common.entity.ApplyAffiliatedUnit;
import com.ruicar.afs.cloud.apply.common.entity.ApplyOrderInfo;
import com.ruicar.afs.cloud.apply.pre.loan.condition.AffiliatedUnitCondition;
import com.ruicar.afs.cloud.apply.pre.loan.mapper.ApplyAffiliatedUnitMapper;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyAffiliatedUnitService;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyOrderInfoService;
import com.ruicar.afs.cloud.apply.pre.loan.vo.AffiliatedUnitVO;
import com.ruicar.afs.cloud.parameter.commom.enums.AffiliatedWay;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;

/**
 * @author: Lee
 * @date: 2020/5/13 21:19
 * @description:
 */
@Service
@AllArgsConstructor
public class ApplyAffiliatedUnitServiceImpl extends ServiceImpl<ApplyAffiliatedUnitMapper, ApplyAffiliatedUnit> implements ApplyAffiliatedUnitService {

    private ApplyOrderInfoService orderInfoService;

    /**
     * 通过ID获取挂靠信息
     *
     * @param id
     * @return
     */
    public ApplyAffiliatedUnit getAffiliatedUnitById(Long id) {

        ApplyAffiliatedUnit affiliatedUnit = this.baseMapper.selectOne(Wrappers.<ApplyAffiliatedUnit>query().lambda().eq(ApplyAffiliatedUnit::getId, id));
        return affiliatedUnit;
    }

    /**
     * 通过申请编号获取挂靠信息
     *
     * @param applyNo
     * @return
     */
    public ApplyAffiliatedUnit getAffiliatedUnitByApplyNo(String applyNo) {

        ApplyAffiliatedUnit affiliatedUnit = this.baseMapper.selectOne(Wrappers.<ApplyAffiliatedUnit>query().lambda().eq(ApplyAffiliatedUnit::getApplyNo, applyNo));
        return affiliatedUnit;
    }

    /**
     * 查询挂靠信息
     *
     * @param applyNo
     * @param affiliatedWay
     * @param vo
     */
    public void getAffiliatedUnit(String applyNo, String affiliatedWay, AffiliatedUnitVO vo) {

        ApplyAffiliatedUnit affiliatedUnit = this.getAffiliatedUnitByApplyNo(applyNo);
        if (affiliatedUnit != null) {
            vo.setApplyNo(affiliatedUnit.getApplyNo());
            if (AffiliatedWay.COMPANY_AFFILIATED.getIndex().equals(affiliatedWay)) {
                ApplyAffiliatedUnit companyInfo = new ApplyAffiliatedUnit();
                companyInfo.setId(affiliatedUnit.getId());
                companyInfo.setAffiliatedId(affiliatedUnit.getAffiliatedId());
                companyInfo.setAffiliatedName(affiliatedUnit.getAffiliatedName());
                companyInfo.setSocUniCrtCode(affiliatedUnit.getSocUniCrtCode());
                companyInfo.setLegalName(affiliatedUnit.getLegalName());
                companyInfo.setLegalPhone(affiliatedUnit.getLegalPhone());
                companyInfo.setLicenceEndDate(affiliatedUnit.getLicenceEndDate());
                companyInfo.setRegistProvince(affiliatedUnit.getRegistProvince());
                companyInfo.setRegistCity(affiliatedUnit.getRegistCity());
                companyInfo.setRegistAddress(affiliatedUnit.getRegistAddress());
                vo.setCompanyInfo(companyInfo);
            } else if (AffiliatedWay.PERSONAL_AFFILIATED.getIndex().equals(affiliatedWay)) {
                ApplyAffiliatedUnit personalInfo = new ApplyAffiliatedUnit();
                personalInfo.setId(affiliatedUnit.getId());
                personalInfo.setAffiliatedName(affiliatedUnit.getAffiliatedName());
                personalInfo.setSocUniCrtCode(affiliatedUnit.getSocUniCrtCode());
                vo.setPersonalInfo(personalInfo);
            } else if (AffiliatedWay.NETWORK_CAR_AFFILIATED.getIndex().equals(affiliatedWay)) {
                ApplyAffiliatedUnit networkInfo = new ApplyAffiliatedUnit();
                networkInfo.setId(affiliatedUnit.getId());
                networkInfo.setAffiliatedId(affiliatedUnit.getAffiliatedId());
                networkInfo.setAffiliatedName(affiliatedUnit.getAffiliatedName());
                networkInfo.setSocUniCrtCode(affiliatedUnit.getSocUniCrtCode());
                networkInfo.setPlatformId(affiliatedUnit.getPlatformId());
                networkInfo.setCooperPlatform(affiliatedUnit.getCooperPlatform());
                networkInfo.setRegistProvince(affiliatedUnit.getRegistProvince());
                networkInfo.setRegistCity(affiliatedUnit.getRegistCity());
                vo.setNetworkInfo(networkInfo);
            }
        }
    }

    /**
     * 保存或更新挂靠信息
     *
     * @param applyNo
     * @param condition
     * @return
     */
    public ApplyAffiliatedUnit saveOrUpdateAffiliatedUnit(String applyNo, AffiliatedUnitCondition condition) {

        ApplyAffiliatedUnit affiliatedUnit = getAffiliatedUnitByApplyNo(applyNo);
        if (affiliatedUnit != null) {
            this.updateAffiliatedUnit(affiliatedUnit, condition);
        } else {
            affiliatedUnit = this.saveAffiliatedUnit(applyNo, condition);
        }
        return affiliatedUnit;
    }

    /**
     * 保存挂靠信息
     *
     * @param condition
     * @return
     */
    public ApplyAffiliatedUnit saveAffiliatedUnit(String applyNo, AffiliatedUnitCondition condition) {

        ApplyAffiliatedUnit affiliatedUnit = new ApplyAffiliatedUnit();
        affiliatedUnit.setApplyNo(applyNo);
        affiliatedUnit.setCarId(condition.getCarId());
        affiliatedUnit.setAffiliatedType(condition.getAffiliatedType());
        if (AffiliatedWay.COMPANY_AFFILIATED.getIndex().equals(condition.getAffiliatedType())) {
            affiliatedUnit.setAffiliatedId(condition.getAffiliatedId());
            affiliatedUnit.setAffiliatedName(condition.getAffiliatedName());
            affiliatedUnit.setSocUniCrtCode(condition.getSocUniCrtCode());
            affiliatedUnit.setLegalName(condition.getLegalName());
            affiliatedUnit.setLegalPhone(condition.getLegalPhone());
            affiliatedUnit.setLicenceEndDate(condition.getLicenceEndDate());
            affiliatedUnit.setRegistProvince(condition.getRegistProvince());
            affiliatedUnit.setRegistCity(condition.getRegistCity());
            affiliatedUnit.setRegistAddress(condition.getRegistAddress());
        } else if (AffiliatedWay.PERSONAL_AFFILIATED.getIndex().equals(condition.getAffiliatedType())) {
            affiliatedUnit.setAffiliatedName(condition.getAffiliatedName());
            affiliatedUnit.setSocUniCrtCode(condition.getSocUniCrtCode());
        } else if (AffiliatedWay.NETWORK_CAR_AFFILIATED.getIndex().equals(condition.getAffiliatedType())) {
            affiliatedUnit.setAffiliatedId(condition.getAffiliatedId());
            affiliatedUnit.setAffiliatedName(condition.getAffiliatedName());
            affiliatedUnit.setSocUniCrtCode(condition.getSocUniCrtCode());
            affiliatedUnit.setPlatformId(condition.getPlatformId());
            affiliatedUnit.setCooperPlatform(condition.getCooperPlatform());
            affiliatedUnit.setRegistProvince(condition.getRegistProvince());// 注册省份
            affiliatedUnit.setRegistCity(condition.getRegistCity());
        }
        this.baseMapper.insert(affiliatedUnit);

        return affiliatedUnit;
    }

    /**
     * 更新挂靠信息
     *
     * @param affiliatedUnit
     * @param condition
     */
    public void updateAffiliatedUnit(ApplyAffiliatedUnit affiliatedUnit, AffiliatedUnitCondition condition) {

        affiliatedUnit.setApplyNo(condition.getApplyNo());
        affiliatedUnit.setAffiliatedType(condition.getAffiliatedType());
        if (AffiliatedWay.COMPANY_AFFILIATED.getIndex().equals(condition.getAffiliatedType())) {
            affiliatedUnit.setAffiliatedId(condition.getAffiliatedId());
            affiliatedUnit.setAffiliatedName(condition.getAffiliatedName());
            affiliatedUnit.setSocUniCrtCode(condition.getSocUniCrtCode());
            affiliatedUnit.setLegalName(condition.getLegalName());
            affiliatedUnit.setLegalPhone(condition.getLegalPhone());
            affiliatedUnit.setLicenceEndDate(condition.getLicenceEndDate());
            affiliatedUnit.setRegistProvince(condition.getRegistProvince());
            affiliatedUnit.setRegistCity(condition.getRegistCity());
            affiliatedUnit.setRegistAddress(condition.getRegistAddress());
            affiliatedUnit.setCooperPlatform("");
            affiliatedUnit.setPlatformId("");
        } else if (AffiliatedWay.PERSONAL_AFFILIATED.getIndex().equals(condition.getAffiliatedType())) {
            affiliatedUnit.setAffiliatedId("");
            affiliatedUnit.setAffiliatedName(condition.getAffiliatedName());
            affiliatedUnit.setSocUniCrtCode(condition.getSocUniCrtCode());
            affiliatedUnit.setLegalName("");
            affiliatedUnit.setLegalPhone("");
            affiliatedUnit.setRegistProvince("");
            affiliatedUnit.setRegistCity("");
            affiliatedUnit.setRegistAddress("");
            affiliatedUnit.setCooperPlatform("");
            affiliatedUnit.setPlatformId("");
        } else if (AffiliatedWay.NETWORK_CAR_AFFILIATED.getIndex().equals(condition.getAffiliatedType())) {
            affiliatedUnit.setAffiliatedId(condition.getAffiliatedId());
            affiliatedUnit.setAffiliatedName(condition.getAffiliatedName());
            affiliatedUnit.setSocUniCrtCode(condition.getSocUniCrtCode());
            Assert.isTrue(StringUtil.isNotEmpty(condition.getCooperPlatform()),"网约车平台不能为空");
            affiliatedUnit.setCooperPlatform(condition.getCooperPlatform());
            affiliatedUnit.setPlatformId(condition.getPlatformId());
            affiliatedUnit.setRegistProvince(condition.getRegistProvince());// 注册省份
            affiliatedUnit.setRegistCity(condition.getRegistCity());
            affiliatedUnit.setLegalName("");
            affiliatedUnit.setLegalPhone("");
            affiliatedUnit.setRegistAddress("");
        }
        affiliatedUnit.setCarId(condition.getCarId());
        affiliatedUnit.setUpdateTime(new Date());
        this.baseMapper.updateById(affiliatedUnit);
        // 更新挂靠方式
        ApplyOrderInfo applyOrderInfo = orderInfoService.getOrderInfoByApplyNo(condition.getApplyNo());
        if(applyOrderInfo != null){
            if(StringUtils.isNotBlank(condition.getBusinessType())){
                applyOrderInfo.setBusinessType(condition.getBusinessType());
            }
            if(StringUtils.isNotBlank(condition.getCarType())){
                applyOrderInfo.setCarType(condition.getCarType());
            }
            if(StringUtils.isNotBlank(condition.getCarNature())){
                applyOrderInfo.setCarNature(condition.getCarNature());
            }
            if(StringUtils.isNotBlank(condition.getOperateWay())){
                applyOrderInfo.setOperateWay(condition.getOperateWay());
            }
            if(StringUtils.isNotBlank(condition.getAffiliatedWay())){
                applyOrderInfo.setAffiliatedWay(condition.getAffiliatedWay());
            }
            if(StringUtils.isNotBlank(condition.getCarPurpose())){
                applyOrderInfo.setCarPurpose(condition.getCarPurpose());
            }
            orderInfoService.updateById(applyOrderInfo);
        }
    }

}
